package com.lanxin.xxform.mappers;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;

import com.lanxin.xxform.beans.Answer;
import com.lanxin.xxform.tools.JetCriteria;

@Mapper
public interface AnswerMapper {

    @Insert("insert into answer (pid,name,phone,createTime,status,uid) values (${pid},#{name},#{phone},${createTime},${status},${uid})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    long insert(Answer answer);

    @Select("select * from answer where id=${id}")
    Answer find(long id);

    @Select("select count(*) from answer ${condition}")
    int count(JetCriteria criteria);

    @Select("select ${select} from answer ${fullCondition}")
    @Results(id = "answerAssemble", value = {
        @Result(id = true, column = "id", property = "id"),
        @Result(property = "pid", column = "pid"),
        @Result(property = "paper", column = "pid", one = @One(
            select = "com.lanxin.xxform.mappers.PaperMapper.find"
        )),
        @Result(property = "answers", column = "id", many = @Many(
            select = "com.lanxin.xxform.mappers.AnswerItemMapper.findAllByAnswer",
            fetchType = FetchType.EAGER
        ))
    })
    List<Answer> findAll(JetCriteria criteria);

    @Update("update answer set status=${status} where id=${id}")
    int update(Answer answer);

    @Delete("delete from answer where id=${id}")
    int delete(long id);
    
}
